package com.example.day8_sp;

import android.os.Environment;
import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

public class HttpUtils {
    public static void download(String path,String filename){
        InputStream stream = null;
        FileOutputStream outputStream = null;
        try {
            URL url = new URL(path);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();

            if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
                stream = connection.getInputStream();//输入流
                File file = Environment.getExternalStorageDirectory();
                outputStream = new FileOutputStream(new File(file,filename));//输出流
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len = stream.read(bytes))!=-1){
                    outputStream.write(bytes,0,len);
                }
            }
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(stream!=null){
                try {
                    stream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
    public static String doPost(String url,String username,String password){
        OutputStream stream;
        InputStream inputStream;
        String param = "username="+username+"&password="+password;
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("content-type","application/json");
            stream = connection.getOutputStream();
            stream.write(param.getBytes(StandardCharsets.UTF_8));
            stream.flush();
            if(connection.getResponseCode()==200){
                inputStream = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder sb = new StringBuilder();
                String str = "";
                while ((str = reader.readLine())!=null){
                    sb.append(str);
                    Log.d("sss", "doPost: "+sb.toString());
                }
                return sb.toString();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
